/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.sample.greetings.swing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import nemos.swing.GenerationException;
import nemos.swing.IComponentFactory;
import nemos.swing.util.ResourceComponentFactory;
import nemos.util.i18n.ITranslator;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;

/**
 * GreetingsPanelFactory : The greeting panel factory.
 * 
 * @author Romain DUBOIS
 */
@Property(name = IComponentFactory.COMPONENT_TYPE, value = "nemos.sample.greetings.panel")
@Component
public class GreetingsPanelFactory extends ResourceComponentFactory<JPanel> {

    /** The greetings panel resource file */
    private static final String GREETINGS_PANEL = "greetingsPanel"; //$NON-NLS-1$

    /** The translator */
    @Reference(name = "translator", referenceInterface = ITranslator.class)
    ITranslator mTranslator;

    /**
     * Constructor.
     */
    public GreetingsPanelFactory() {
        super(JPanel.class);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.util.ResourceComponentFactory#createComponent(java.util.Map)
     */
    @Override
    public JPanel createComponent(final Map<String, ?> pInitParams) {
        try {
            loadComponents(pInitParams);
            final JTextField lTextField = getComponent("text", JTextField.class); //$NON-NLS-1$
            final JButton lButton = getComponent("button", JButton.class); //$NON-NLS-1$
            final JPanel lPanel = getComponent("panel", JPanel.class); //$NON-NLS-1$

            lButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(@SuppressWarnings("unused") final ActionEvent e) {
                    lButton.setEnabled(false);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            final ITranslator lTranslator = mTranslator;
                            if (lTranslator != null) {
                                final String lMessage = lTranslator.getLabel(
                                        SwingGreetingsMessage.GREETINGS, lTextField.getText());

                                JOptionPane.showMessageDialog(null, lMessage);
                            }
                            lButton.setEnabled(true);
                        }
                    }).start();
                }
            });

            return lPanel;
        } catch (final GenerationException e) {
            return createDefault(e, null);
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.util.ResourceComponentFactory#getResourceName(java.util.Map)
     */
    @Override
    protected String getResourceName(@SuppressWarnings("unused") final Map<String, ?> pInitParams) {
        return GREETINGS_PANEL;
    }

    /**
     * Bind the translator.
     * 
     * @param pTranslator
     *            The translator
     */
    protected void bindTranslator(final ITranslator pTranslator) {
        mTranslator = pTranslator;
    }

    /**
     * Unbind the translator.
     * 
     * @param pTranslator
     *            The translator
     */
    protected void unbindTranslator(@SuppressWarnings("unused") final ITranslator pTranslator) {
        mTranslator = null;
    }
}
